package com.stable.web.controller;

import com.stable.service.BasicFindService;
import com.stable.utils.FileUtil;
import com.stable.vo.bus.StockBaseInfo;
import com.stable.vo.http.JsonResult;
import com.stable.vo.spi.req.EsQueryPageReq;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 基础数据过滤查询
 */
@RequestMapping("/basicFind")
@RestController
@Log4j2
public class BasicFindController {

    @Autowired
    private BasicFindService basicFindService;


    /**
     * 基础数据过滤查询
     *
     * @param code        股票代码
     * @param name        股票名称
     * @param industry    所属行业
     * @param sshy        申万所属行业
     * @param conceptName 概念关键字
     * @param gsld        公司亮点
     * @return
     */
    @RequestMapping(value = "/findList", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> stocklist(String code,
                                                String name,
                                                String industry,
                                                String sshy,
                                                String conceptName,  //相关概念
                                                String gsld,  //公司亮点
                                                EsQueryPageReq queryPage) {
        queryPage.setPageSize(1000);
        JsonResult r = new JsonResult();
        try {
            r.setResult(basicFindService.queryBasicFind(code, name, industry, sshy, conceptName, gsld, queryPage));
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }


    /**
     * 下载文件
     *
     * @return
     */
    @RequestMapping({"/download"})
    public void download(String code,
                         String name,
                         String industry,
                         String sshy,
                         String conceptName,  //相关概念
                         String gsld,  //公司亮点
                         EsQueryPageReq queryPage) {
        queryPage.setPageSize(1000);
        JsonResult r = new JsonResult();
        List<StockBaseInfo> newFinanceReturnInfos = basicFindService.queryBasicFind(code, name, industry, sshy, conceptName, gsld, queryPage);

        String filename = "基础数据过滤查询列表.txt";
        FileUtil.downloadFile(newFinanceReturnInfos.stream().map(StockBaseInfo::getCode).collect(Collectors.toList()), filename);
    }

}
